use alloc::vec::Vec;
use core::convert::TryInto;

pub fn get_code(sn: u16, id: &[u8], key: &[u8]) -> [u8; 8] {
    let mut buf = Vec::from(sn.to_be_bytes());
    buf.extend_from_slice(id);
    buf.extend_from_slice(key);
    let digest: [u8; 16] = md5::compute(buf).into();
    let a: u64 = u64::from_be_bytes(digest[0..8].try_into().unwrap());
    let b: u64 = u64::from_be_bytes(digest[8..16].try_into().unwrap());
    (a ^ b).to_be_bytes()
}
#[test]
fn test() {
    let key = get_code(1, &[0x82, 0x11, 0x01, 0x01], &[0x12, 0x34, 0x56, 0x78]);
    assert_eq!([0xe2, 0x97, 0x51, 0xf2, 0x30, 0xbf, 0x6b, 0x57], key);
}
